<?

/**
* local collection functions of tocmenu_45
*
* @author Adesanto Asman
* @version 1.0
* @license Free
* @copyright Copyright (c)2007, Adesanto Asman
* @package CLocal
*
*/

define("_DEFAULT_START"   , 0);
define("_SN_HEAD"         , 0);
define("_SN_TAIL"         , 1);

/**
* @package CLocal
*
*/
class CLocal{

  /**
   * process SN
   *
   * @param array       $source
   * @param integer     $iRow
   * @return array
   */
  function procSN(&$source,
                  &$iRow){

    $model      = $source["modelEnt"][$iRow];
    $qty        = $source["qtyEnt"][$iRow];
    $strStart   = $source["headEnt"][$iRow];
    $strLength  = $source["lengthEnt"][$iRow];
    $srcSN      = $source["serNumEnt"][$iRow];

    $strLength -= $strStart;

    /**
     * extract the SN
     */
    $SN = explode("-", $srcSN);

    $theSerial = substr($SN[_SN_HEAD],
                        _DEFAULT_START,
                        $strStart);

    $theNumber = substr($SN[_SN_HEAD],
                        $strStart,
                        $strLength);

    for($iNum = 0; $iNum < $qty; $iNum++){

      $tmpNum = (int)$theNumber + (int)$iNum;

      $chkSn[$iNum] = $theSerial.
                      Misc::addDigit2($strLength,
                                      (string)$tmpNum).
                      $tmpNum;
    }

    return $chkSn;
  }

  /**
   * check the SN into DB
   * Make sure that there is no duplicate
   *
   * @param Misc      $Misc
   * @param integer   $qty
   * @param integer   $strStart
   * @param integer   $strLength
   * @param string    $srcSN
   * @param Database  $Db
   * @param string    $srcDb
   * @return string on success return booelan FALSE, fail return the serial number
   */
  function isSnValid($Misc,
                     $qty,
                     $strStart,
                     $strLength,
                     $srcSN,
                     $Db,
                     $srcDb){

    /**
     * the number length
     * eg. BC010 with head: 2 and total length 5
     *     the number length would be 5-2 = 3
     *     thus we have BC : 2 and 010 : 3
     */
    $strLength -= $strStart;

    /**
     * extract the SN
     */
    $SN = explode("-", $srcSN);

    $theSerial = substr($SN[_SN_HEAD],
                        _DEFAULT_START,
                        $strStart);

    $theNumber = substr($SN[_SN_HEAD],
                        $strStart,
                        $strLength);

    for($iNum = 0; $iNum < $qty; $iNum++){

      $tmpNum = (int)$theNumber + (int)$iNum;

      $chkSn = $theSerial.
               $Misc->addDigit2($strLength,
                                (string)$tmpNum).
               $tmpNum;

      GlobalFetchers::fetchIsSnValid($Db,
                                     $chkSn,
                                     $srcDb,
                                     $isValid);

      if($isValid == 1){

        /* Continue the process/Loop */
      }else{

        return $chkSn;
      }
    }

    return false;
  }
}

?>